#include "logfileparser.h"

/*
port2.tserver2.ucf.edu - - [01/Jul/1995:13:52:10 -0400] "GET /images/MOSAIC-logosmall.gif HTTP/1.0" 200 363
port2.tserver2.ucf.edu - - [01/Jul/1995:13:52:11 -0400] "GET /images/NASA-logosmall.gif HTTP/1.0" 200 786
gate.ntrs.com - - [01/Jul/1995:13:52:13 -0400] "GET /shuttle/missions/sts-71/movies/sts-71-tcdt-crew-walkout.mpg HTTP/1.0" 200 106496
port2.tserver2.ucf.edu - - [01/Jul/1995:13:52:14 -0400] "GET /images/USA-logosmall.gif HTTP/1.0" 200 234
halifax-ts4-05.nstn.ca - - [01/Jul/1995:13:52:14 -0400] "GET /shuttle/technology/sts-newsref/stsref-toc.html HTTP/1.0" 200 84907
*/

LogFileParser::LogFileParser()
{
    stream = 0;
    file = 0;
    requests = 0;
}

LogFileParser::~LogFileParser()
{
    delete stream;
    delete file;
}

void LogFileParser::setFilename(QString filename)
{
    this->filename = filename;
}

void LogFileParser::openStream()
{
    // Open file if exists
    if(file != 0) {
        delete file;
    }
    file = new QFile(this->filename);

    file->open(QIODevice::ReadOnly);
    qDebug() << this->filename;

    if(stream != 0) {
        delete stream;
        qDebug() << "Pointer stream has been release.";
    }
    stream = new QTextStream(file);
    qDebug() << "Pointer stream has been alocated.";
}

void LogFileParser::findAllExtensions()
{
    //
}

void LogFileParser::removeJunks()
{
    // This method removes all unnecessary lines

    if(requests != 0)
        delete requests;
    requests = new QList<Request*>();

    // Read line by line and remove junks
    QString line = "";
    while(!stream->atEnd()) {
        line = stream->readLine();
        // Remove junks

        QStringList param = line.split(" ");
        //qDebug() << param;
        // URLs with .html or / at it's end are allowed
        if(param.at(6).indexOf(".html") != -1 || param.at(6).mid(param.at(6).length() - 1, 1) == "/") {
            // GET requests are allowed only
            if(param.at(5) == "\"GET") {
                // Requests with status code 200 are allowed
                if(param.at(8) == "200") {
                    //qDebug() << line;
                    Request *req = new Request(line);
                    requests->append(req);
                    //qDebug() << req.host() << req.timestamp() << req.page();
                }
            }
        }

        // Links with GET are allowed
        //qDebug() << line;
    }

    // Reverse list
    //reverseList(requests);
}

QList<Request*> *LogFileParser::getRequests()
{
    return requests;
}

void LogFileParser::reverseList(QList<Request *> *list)
{
    int size = list->size();

    for(int i = 0; i < size / 2; ++i)
        list->swap(i, size-i-1);
}

void LogFileParser::debug()
{
    qDebug() << requests->size();

    //for(int i = 0; i < requests->size(); ++i)
    //{
    //    qDebug() << requests->at(i)->host() << requests->at(i)->timestamp();// << requests->at(i)->page();
    //}

    //for(int i = 0; i < uniqueHosts->size(); ++i)
    //{
    //    qDebug() << uniqueHosts->at(i);
    //}
}
